<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DawnSql编辑器</title>
    <link href="/static/js/resources/ext-theme-neptune/ext-theme-neptune-all.css" rel="stylesheet"/>
    <script src="/static/js/ext-all.js" type="text/javascript"></script>
    <script src="/static/js/locale/ext-lang-zh_CN.js" type="text/javascript"></script>
    <script src="/static/js/MyUtils.js" type="text/javascript"></script>

    <link href="/static/codemirror/lib/codemirror.css" rel="stylesheet"/>
    <link href="/static/codemirror/addon/hint/show-hint.css" rel="stylesheet"/>
    <link href="/static/codemirror/theme/idea.css" rel="stylesheet"/>

    <script src="/static/codemirror/lib/codemirror.js" type="text/javascript"></script>
    <script src="/static/codemirror/mode/sql/sql.js" type="text/javascript"></script>
    <script src="/static/codemirror/addon/hint/show-hint.js" type="text/javascript"></script>
    <script src="/static/codemirror/addon/hint/sql-hint.js" type="text/javascript"></script>
    <script src="/static/codemirror/addon/selection/active-line.js" type="text/javascript"></script>

    <style type="text/css">
        .indexicon {
            background-image: url(/static/page_img/application_home.png) !important;
        }
        .applicationIcon {
            background-image:url(/static/page_img/application_home.png) !important;
         }

        .application_doubleIcon {
            background-image:url(/static/page_img/application_double.png) !important;
         }

        .application_cascadeIcon {
            background-image:url(/static/page_img/application_cascade.png) !important;
         }

        .tbar_synchronizeIcon {
            background-image:url(/static/page_img/tbar_synchronize.png) !important;
         }

        .t1{
		background-color:#ebf7ff; width:115px; border-bottom:1px solid #b5e2ff; border-right:1px solid #b5e2ff;
		font-size: 12px;
		}
		.t2{
		width:200px; border-bottom:1px solid #b5e2ff; border-right:1px solid #b5e2ff;
		font-size: 12px;
		}
    </style>

    <script type="text/javascript">
        var localStore = window.localStorage;
        // 解决Iframe IE加载不完全的问题
        function endIeStatus() { }

        var work_id = '{{ m.work_id }}';

        var ShowWin;

        var storage = new myLocalStore();
        //Ext.DomHelper.append(document.body, {tag: 'textarea', id: 'code_area'});

        var editor;

        function my_refresh () {
            editor = CodeMirror.fromTextArea(document.getElementById('code_area'), {//根据DOM元素的id构造出一个编辑器
                               lineNumbers: true,
                               styleActiveLine: true,
                               extraKeys: {'Ctrl': 'autocomplete'},
                               theme: "idea",//设置主题
                               //mode: 'text/x-mysql',
                               mode : 'text/x-plsql',
                               hintOptions: {tables: Ext.JSON.decode(localStore.getItem('tables'))}
                           });
                           //editor.setSize('auto', '227px');
                           editor.on("keyup", function (cm, event) {
                               if (!cm.state.completionActive//所有的字母和'$','{','.'在键按下之后都将触发自动完成
                                   && ((event.keyCode >= 65 && event.keyCode <= 90)
                                       || event.keyCode == 52 || event.keyCode == 219
                                       || event.keyCode == 190)) {
                                   CodeMirror.commands.autocomplete(cm, null, {
                                       completeSingle: false
                                   });
                               }

                               if (work_id == '')
                               {
                                   storage.set_default_work(editor.getValue());
                               }
                               else
                               {
                                   storage.update_my_work_code(work_id, editor.getValue());
                               }
                           });

                           //editor.on('cursorActivity', function (cm, event) {
                           //});

                           if (work_id == '')
                           {
                               var my_code = storage.get_default_work();
                               if (my_code != null && my_code != '') {
                                   editor.setValue(my_code);
                               }
                           }
                           else
                           {
                               var my_code = storage.get_work(work_id);
                               if (my_code != null && my_code != '') {
                                   editor.setValue(my_code);
                               }
                           }
        }

       var code_area_panel = Ext.create('Ext.panel.Panel', {
           anchor: '100%',
           resizable: {
               handles: 's'
           },
           listeners: {
               resize: function(obj, width, height, oldWidth, oldHeight, eOpts){
                   if (typeof(editor) != 'undefined' && editor)
                   {
                       editor.setSize('auto', height + 'px');
                   }
               }
           },
           contentEl: 'code_area'
       });

       //var source = {"users": ["id", "name"], "dbs": ["id", "name"]};
       //var source = Ext.JSON.decode(localStore.getItem('tables'));

        var edit_form = Ext.create('Ext.form.Panel', {
           /**
            * 定义 eidt form
            * */
           id: 'edit_form',
           itemId: 'edite_win',
           layout: "form",
           defaults: {
               anchor: '100%'
           },
           buttonAlign: "center",
           labelAlign: "right",
           items: [code_area_panel, {
               id: 'p_grid',
               itemId: 'p_grid',
               //bodyStyle :'overflow-x:hidden;overflow-y:scroll',
               bodyStyle :'overflow-x:scroll;overflow-y:scroll',
               border: true,
               hidden: true
           }, {
               itemId: 'p_error',
               hidden: true
           }],
           listeners: {
               "afterrender": function (m) {
               }
           }
       });

        function run_sql()
        {
            var vs = Ext.String.trim(editor.getSelection());
           if (vs == '') {
               vs = Ext.String.trim(editor.getValue());
           }

           if (vs == '')
           {
               return;
           }

           if (my_single_select(vs))
           {
               var store_url = '/run_select_sql/?user_token={{ m.user_token }}';
               Ext.Msg.wait('正在运行，请稍候...', '信息提示');
               Ext.Ajax.request({
                   url: '/run_sql/',
                   method: 'POST',
                   params: {
                       user_token: '{{ m.user_token }}',
                       select: 1,
                       code: vs
                   },
                   success: function (response) {
                       Ext.Msg.hide();
                       var result = Ext.decode(response.responseText);
                       if (result.hasOwnProperty('err')) {
                           var p_grid = edit_form.getComponent('p_grid');
                           p_grid.hide();
                           var p_error = edit_form.getComponent('p_error');
                           p_error.removeAll();
                           p_error.add(Ext.create('Ext.Component', {
                               html: '<span style="color: red; font-weight: bolder;">' + result.err + '</span>'
                           }));
                           p_error.show();
                       }
                       else
                       {
                           console.log(response.responseText);
                           var store = new Ext.data.JsonStore({
                                proxy:{
                                    type: 'ajax',
                                    url: store_url,
                                    actionMethods: {
                                        read: 'POST'
                                    },
                                    reader: {
                                        type: 'json',
                                        root: "root",
                                        totalProperty: "totalProperty"
                                    }
                                },
                                pageSize: 50,
                                //remoteSort: true,
                                fields: result.columns_name
                           });

                           store.on('beforeload', function (store, operation, eOpts) {
                                Ext.apply(store.proxy.extraParams, {code: vs});
                           });

                           store.load({
                                params: {
                                    code: vs,
                                    start: 0,
                                    limit: 50
                                },
                                callback: function(records, operation, success) {
                                    if (records.length == 1 && records[0].data.hasOwnProperty('err'))
                                    {
                                        var err = records[0].data['err'];
                                        if (err != '') {
                                            var p_grid = edit_form.getComponent('p_grid');
                                            p_grid.hide();
                                            var p_error = edit_form.getComponent('p_error');
                                            p_error.removeAll();
                                            p_error.add(Ext.create('Ext.Component', {
                                                html: '<span style="color: red; font-weight: bolder;">' + err + '</span>'
                                            }));
                                            p_error.show();
                                        }
                                    }
                                    else
                                    {
                                        var p_error = edit_form.getComponent('p_error');
                                        p_error.removeAll();
                                        p_error.hide();
                                    }
                                }
                           });

                           var grid = new Ext.grid.GridPanel({
                                autoHeight: true,
                                stripeRows: true,
                                store: store,
                                columns: result.columns,
                                //autoScroll: true,
                                border: true,
                                viewConfig: {
                                    columnsText: "显示/隐藏列",
                                    sortAscText: "正序排列",
                                    sortDescText: "倒序排列",
                                    forceFit: true
                                },
                                loadMask: {
                                    msg: '正在加载数据，请稍等．．．'
                                },
                                //bbar: bbar,
                                dockedItems: [{
                                    xtype: 'pagingtoolbar',
                                    store: store,   // same store GridPanel is using
                                    dock: 'bottom',
                                    displayInfo: true
                                }]
                            });

                           var p_grid = edit_form.getComponent('p_grid');
                           p_grid.removeAll();
                           p_grid.add(grid);
                           p_grid.show();

                            grid.on('itemdblclick', function (v, record, item, index, e, eOpts) {
                                var lst = [];
                                for (var m in record.data)
                                {
                                    lst.push({name: m, value: record.data[m]});
                                }
                                //console.log(lst);
                                window.parent.show_win({data: lst});
                            });
                       }
                   },
                   failure: function (response) {
                       Ext.Msg.hide();

                       var result = Ext.decode(response.responseText);
                       console.log(response.responseText);
                       Ext.Msg.show({
                           title: '错误提示',
                           msg: result.msg,
                           buttons: Ext.Msg.OK,
                           minWidth: 100
                       });
                   }
               });
           }
           else
           {
               Ext.Msg.wait('正在运行，请稍候...', '信息提示');
               Ext.Ajax.request({
                   url: '/run_sql/',
                   method: 'POST',
                   params: {
                       user_token: '{{ m.user_token }}',
                       code: vs
                   },
                   success: function (response) {
                       Ext.Msg.hide();
                       var result = Ext.decode(response.responseText);
                       console.log(response.responseText);

                       var p_grid = edit_form.getComponent('p_grid');
                       p_grid.hide();
                       var p_error = edit_form.getComponent('p_error');
                       p_error.removeAll();
                       if (result.msg != null)
                       {
                           p_error.add(Ext.create('Ext.Component', {
                               html: result.msg
                           }));
                       }
                       p_error.show();
                   },
                   failure: function (response) {
                       Ext.Msg.hide();

                       var result = Ext.decode(response.responseText);
                       console.log(response.responseText);
                       Ext.Msg.show({
                           title: '错误提示',
                           msg: result.msg,
                           buttons: Ext.Msg.OK,
                           minWidth: 100
                       });
                   }
               });
           }
        }

        function load_to_db()
        {
            var vs = Ext.String.trim(editor.getSelection());
           if (vs == '') {
               vs = Ext.String.trim(editor.getValue());
           }

           if (vs == '')
           {
               return;
           }

           Ext.Msg.wait('正在保存代码，请稍候...', '信息提示');
               Ext.Ajax.request({
                   url: '/load_code/',
                   method: 'POST',
                   params: {
                       user_token: '{{ m.user_token }}',
                       code: vs
                   },
                   success: function (response) {
                       Ext.Msg.hide();
                       var result = Ext.decode(response.responseText);

                       var p_grid = edit_form.getComponent('p_grid');
                       p_grid.hide();
                       var p_error = edit_form.getComponent('p_error');
                       p_error.removeAll();
                       if (result.msg != null)
                       {
                           p_error.add(Ext.create('Ext.Component', {
                               html: result.msg
                           }));
                       }
                       p_error.show();
                   },
                   failure: function (response) {
                       Ext.Msg.hide();

                       var result = Ext.decode(response.responseText);
                       Ext.Msg.show({
                           title: '错误提示',
                           msg: result.msg,
                           buttons: Ext.Msg.OK,
                           minWidth: 100
                       });
                   }
               });
        }

        Ext.onReady(function () {

             Ext.create('Ext.panel.Panel', {
                id: "panel_panel",
                //region: "center",
                 layout: 'fit',
                renderTo: Ext.getBody(),
                items: [edit_form],
                   listeners: {
                       afterrender: function (obj) {
                           document.getElementById('code_area').style = '';
                           editor = CodeMirror.fromTextArea(document.getElementById('code_area'), {//根据DOM元素的id构造出一个编辑器
                               lineNumbers: true,
                               styleActiveLine: true,
                               extraKeys: {'Ctrl': 'autocomplete'},
                               theme: "idea",//设置主题
                               //mode: 'text/x-mysql',
                               mode : 'text/x-plsql',
                               hintOptions: {tables: Ext.JSON.decode(localStore.getItem('tables'))}
                           });
                           //editor.setSize('auto', '227px');
                           editor.on("keyup", function (cm, event) {
                               if (!cm.state.completionActive//所有的字母和'$','{','.'在键按下之后都将触发自动完成
                                   && ((event.keyCode >= 65 && event.keyCode <= 90)
                                       || event.keyCode == 52 || event.keyCode == 219
                                       || event.keyCode == 190)) {
                                   CodeMirror.commands.autocomplete(cm, null, {
                                       completeSingle: false
                                   });
                               }

                               if (work_id == '')
                               {
                                   storage.set_default_work(editor.getValue());
                               }
                               else
                               {
                                   storage.update_my_work_code(work_id, editor.getValue());
                               }
                           });

                           if (work_id == '')
                           {
                               var my_code = storage.get_default_work();
                               if (my_code != null && my_code != '') {
                                   editor.setValue(my_code);
                               }
                           }
                           else
                           {
                               var my_code = storage.get_work(work_id);
                               if (my_code != null && my_code != '') {
                                   editor.setValue(my_code);
                               }
                           }
                       }
                   },
                   bodyStyle :'overflow-x:hidden;overflow-y:scroll',
                   hidden: false
            });
        });

    </script>
</head>
<body>
<textarea id="code_area" style="display: none;">
</textarea>
<a href="javascript:void(0);" onclick="javascript:endIeStatus();" id="endIeStatus" style="display: none;" />
<div id="dawn_sql_show1"></div>
</body>
</html>







































